<script lang="ts">
export default {
  name: 'appLogList'
};
</script>

<script setup lang="ts">
import router from '@/router';
import Edit from './components/edit.vue'
import { formatMillisecondsToDate, aDownloadFile } from '@/utils/utils'
import {
  appFlowOathList,
} from '@/api/app';
import { getDictPage } from '@/api/dict'


const queryFormRef = ref(ElForm); // 查询表单
const formRef = ref(ElForm); // 用户表单
const defaultTime: [Date, Date] = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
];
const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams: any = reactive({
  current: 1,
  pageSize: 10,
  beginTime: '',
  endTime: '',
  status: null
});

const formData = reactive({
  status: 1,

});

const dialog = reactive<any>({
  visible: false,
});

const dataList = ref([]);
const operateList = ref<any>([]);

/**
 * 查询
 */
function handleQuery() {
  appFlowOathList({
    ...queryParams,
    status: Number(queryParams.status),

  })
    .then((res: any) => {
      dataList.value = res.data.data;
      total.value = res.data.total;
    })
}
/**
 * 重置查询
 */
function resetQuery() {
  queryParams.beginTime = '';
  queryParams.endTime = '';
  queryParams.current = 1;
  queryFormRef.value.resetFields();
  handleQuery();
}


/**
 * 行checkbox change事件
 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}


/**
 * 重置表单
 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
}

/**
 * 字典查询
 */
function getDict() {
  getDictPage({
    current: 1,
    pageSize: 9999,
    parentId: 171
  }).then((res: any) => {
    operateList.value = res.data.data
  })
}

/**
 * 类型表单弹窗
 *
 */
function openDialog(row?: object) {
  dialog.visible = true;
  if (row) {
    dialog.title = '查看';
    Object.assign(formData, row);
  } else {
    resetForm();
    dialog.title = '查看';
  }
}

onMounted(async () => {
  getDict()
  handleQuery(); // 初始化用户列表数据
});

watch(
  () => queryParams.startEndTime,
  (val: any) => {
    if (Array.isArray(val)) {
      queryParams.beginTime = val[0];
      queryParams.endTime = val[1];
    } else {
      queryParams.beginTime = '';
      queryParams.endTime = '';
    }
  }
);


</script>

<template>
  <div class="app-container">

    <el-row :gutter="24">
      <el-col :lg="24" :xs="24">
        <div class="search">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item prop="startEndTime" label="操作时间">
              <el-date-picker v-model="queryParams.startEndTime" type="datetimerange" range-separator="至"
                start-placeholder="开始时间" end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss"
                :default-time="defaultTime" />
            </el-form-item>
            <el-form-item prop="operateType" label="执行状态">
              <el-select v-model="queryParams.operateType" placeholder="请选择">
                <el-option v-for="item in operateList" :key="item.name" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleQuery">
                <i-ep-search />搜索
              </el-button>
              <el-button @click="resetQuery">
                <i-ep-refresh />
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="never">
          <template #header>
            <div class="flex justify-between">
              <div>

              </div>
            </div>
          </template>

          <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="index" width="100" />
            <el-table-column label="演示时间" align="center">
              <template #default="scope">
                {{ formatMillisecondsToDate(scope.row.createdAt) }}
              </template>
            </el-table-column>
            <el-table-column label="在线状态" align="center">
              <template #default="scope">
                <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
                <el-tag v-else="scope.row.status == 2" type="danger">异常</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" align="left" width="200">
              <template #default="scope">
                <el-button type="primary" link size="small" @click.stop="openDialog(scope.row)">
                  <i-ep-view />查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.current"
            v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
        </el-card>
      </el-col>
    </el-row>
    <Edit :dialog="dialog" :formData="formData" @resetQuery="resetQuery"></Edit>
  </div>
</template>
<style lang="scss" scoped>
.app-container {
  .mb-4 {
    .data-box {
      background: white;
      height: 130px;
      display: flex;
      justify-content: space-between;
      padding: 20px;

      .data-box-info {
        .data-box-info-value {
          text-align: center;
        }

        .data-box-info-name {
          text-align: center;
        }

        .data-box-info-title {
          font-size: 20px;
          font-weight: bold;
          margin-bottom: 10px;
        }
      }
    }
  }
}
</style>